package Persondata;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.hibernate.Session;

import db.HibernateHelper;

public class PersonEdit extends WebPage {
			
	public PersonEdit(final PageParameters params) {
		// Load Person
		
		Session session = HibernateHelper.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Object ret = session.get(Person.class, params.getInt("idInternal"));
		if (ret == null) {
			// Fehler: Zurück zur Übersicht
			setResponsePage(PersonWeb.class);
			return;
		}
		Person p = (Person)ret;
		
//		session.getTransaction()
		
		final CompoundPropertyModel<Person> model = new CompoundPropertyModel<Person>(p);
		
		// Panel einfügen
		add(new PersonFormPanel("editPersonFormular", model, "Person aktualisieren") {
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmitModelHandle(IModel<Person> model) {
				Session session = HibernateHelper.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.saveOrUpdate(model.getObject());
				session.getTransaction().commit();
				//session.close();
				this.setResponsePage(PersonWeb.class, params);
			}
		});
		
	}

	
	

}
